﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Web.Mvc;


namespace CampusSite.Security
{

    public class RolActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            dynamic viewBag = filterContext.Controller.ViewBag;
            viewBag.UserRol = string.Empty;

            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                Models.UserRepository userRepository = new Models.UserRepository(new Models.CampusSiteContext());


                if (userRepository.IsUserInRole(filterContext.HttpContext.User.Identity.Name, Models.Constantes.RolAdministrador))
                {
                    viewBag.UserRol = Models.Constantes.RolAdministrador;
                }
                else
                {
                    viewBag.UserRol = Models.Constantes.RolResponsable;
                }

                if (filterContext.HttpContext.Request.UrlReferrer != null)
                    viewBag.PreviousURL = filterContext.HttpContext.Request.UrlReferrer.ToString();
                
                userRepository.Dispose();
            }
        }
    }
}